package com.scpii.api.common.auth.token;

import org.springframework.security.core.context.SecurityContextHolder;

import com.scpii.api.common.auth.AuthorizationRequest;
import com.scpii.api.common.auth.ClientAuthentication;
import com.scpii.api.common.auth.client.ClientDetailsService;

public class ClientCredentialsTokenGranter extends AbstractTokenGranter {

	public static final String GRANT_TYPE = "client";

	public ClientCredentialsTokenGranter(
			AuthorizationServerTokenServices tokenServices,
			ClientDetailsService clientDetailsService) {
		super(tokenServices, clientDetailsService, GRANT_TYPE);
	}

	@Override
	protected ClientAuthentication getClientAuthentication(
			AuthorizationRequest clientToken) {
		return new ClientAuthentication(clientToken, SecurityContextHolder
				.getContext().getAuthentication());
	}

	@Override
	public AuthAccessToken grant(String grantType,
			AuthorizationRequest authorizationRequest) {
		AuthAccessToken token = super.grant(grantType, authorizationRequest);
		if (token != null) {
			DefaultAccessToken norefresh = new DefaultAccessToken(token);
			norefresh.setRefreshToken(null);
			token = norefresh;
		}
		return token;
	}

}
